package cn.kgc.houseservice.service;

import cn.kgc.bean.Manage;
import cn.kgc.myhousemapper.mapper.ManageMapper;
import cn.kgc.service.ManageService;
import cn.kgc.utils.CrowdFundingUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Service
@Component
public class ManageServiceImpl implements ManageService {
    @Resource
    ManageMapper manageMapper;
    @Override
    public Manage selectManageForHouse(Integer houseid) {
/*        ManageExample manageExample=new ManageExample();
        ManageExample.Criteria criteria = manageExample.createCriteria();
        criteria.andHouseidEqualTo(houseid);
        criteria.andIszEqualTo(1);*/
        List<Manage> manages = manageMapper.selectList(new QueryWrapper<Manage>().eq("isz",1).eq("houseid",houseid));
        if (manages.size()>0){
            return manages.get(0);
        }else{
            return null;
        }
    }

    @Override
    public int update(Manage manage) {
        return manageMapper.updateById(manage);
    }

    @Override
    public PageInfo<Manage> getManages(Integer pageNum, Integer pageSize, Integer houseid, String name,Integer state,Integer id) {
        PageHelper.startPage(pageNum,pageSize);
/*        ManageExample manageExample=new ManageExample();
        ManageExample.Criteria criteria = manageExample.createCriteria();*/
        QueryWrapper<Manage> queryWrapper=new QueryWrapper<>();
        if (houseid!=null&&houseid!=0){
            queryWrapper.eq("houseid",houseid);
        }
        if (name!=null&&!name.equals("")){
            queryWrapper.like("name",name);
        }
        if (state!=null&&state<=2){
            queryWrapper.eq("state",state);
        }
        if (id!=null&&id!=0){
            queryWrapper.eq("id",id);
        }
        List<Manage> manages = manageMapper.selectList(queryWrapper);
        PageInfo<Manage> pageInfo=new PageInfo<>(manages);
        return pageInfo;
    }

    @Override
    public int updateManage(Manage manage) {
        return manageMapper.updateById(manage);
    }

    @Override
    public int deleteManage(Integer id) {
        return manageMapper.deleteById(id);
    }

    @Override
    /*1 管理员已存在 0 增加失败 2成功*/
    public int addManage(Manage manage) {
/*        ManageExample manageExample=new ManageExample();
        ManageExample.Criteria criteria = manageExample.createCriteria();
        criteria.andUsernameEqualTo(manage.getUsername());*/
        List<Manage> manages = manageMapper.selectList(new QueryWrapper<Manage>().eq("username",manage.getUsername()));
        if (manages.size()>0){
            return 1;
        }else{
            manage.setPassword(CrowdFundingUtils.md5(manage.getPassword()));
            int insert = manageMapper.insert(manage);
            if (insert>0){
                return 2;
            }else{
                return 0;
            }

        }

    }
}
